home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 955 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: allocating unlimited string input....HELP
  5. Date: 10 Jan 1996 14:38:19 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4d0j0r$7aq@news.iag.net>
  8. References: <4cvph6$s8s@pulp.ucs.ualberta.ca>
  9. NNTP-Posting-Host: pm1-orl23.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <4cvph6$s8s@pulp.ucs.ualberta.ca>, ryangall@gpu.srv.ualberta.ca 
  13. says...
  14. >
  15. >I am doing an assignment for my computing class, and seem to have run into 
  16. >a small problem. I am supposed to be able to read an unlimited string from 
  17. >stdin, without defining maximum size. I thought that this problem would be 
  18. >simple....but I have been at it for a while now. Ill show you what my last 
  19. >resort was.....
  20. >
  21. >
  22. >int read(char *S)
  23. >{
  24. > char *A,*B;
  25. > char ch,count=0;
  26. >
  27. >  A=NULL;
  28. >  B=NULL;
  29. >
  30. >  ch=getc(stdin);
  31. >
  32. >  while(ch!='\n')
  33. >  {
  34. >    B=(char *) malloc(count+2);
  35. >    if(count>0)
  36. >    { 
  37. >      sprintf(B,"%s%c",A,ch);
  38. >      free(A);
  39. >    }
  40. >    else sprintf(B,"%c",ch);
  41. >
  42. >    printf("%s---%i----%i\n",B,strlen(B),count);
  43. >    A=B;
  44. >    ch=getc(stdin);
  45. >    count=count+1;
  46. >  }
  47. >
  48. >}
  49. >
  50. <snip>
  51. >Anyways, the function reads in just fine, but for some reason when it gets 
  52. >to 146 characters it bails out, loses char *B and starts reducing 
  53.  
  54. Are you sure it doesn't start acting up, when count reaches 128?  That's 
  55. the result where char is a signed char.  At 127 + 1, count goes negative 
  56. (becomes -128).  Subsequent loops would count up from there.
  57.  
  58. Of course, the first malloc call with a negative size causes a failure, but 
  59. your code isn't testing for allocation failure.  So you don't catch it
  60. there.  Instead you start using invalid pointers in your sprintf and free 
  61. calls.  From that point on anything can happen.
  62.  
  63. You should probably see count go negative in the output from your printf.  
  64. Unfortunately, your format string used '-' between the fields, so the 
  65. negative sign simply blends in.
  66.  
  67. So you need to:
  68.  
  69.    1. define count as a larger type.
  70.    2. add a condition to your loop to ensure that count doesn't exceed
  71.       the maximum for its type (look in limits.h for macros).
  72.    3. add an allocation failure test (if(B==NULL) {/* handle fail*/}).
  73.    4. modify the format string in your printf.
  74.    5. define ch as int (the correct return type for getc).
  75.  
  76. You might want to:
  77.    1. loop into the realloc function.  It is made for this type of code.
  78.    2. d/l and read the c.l.c faq (Frequently Asked Question) list.  It is 
  79.       available for anonymous ftp from rtfm.mit.edu /pub/usenet/comp.lang.c.
  80.    3. remove the cast from the malloc return.  It is not necessary in ansi
  81.       c and can hide some errors.
  82.    4. add ch == EOF as a break condition for your loop.
  83.  
  84. >please mail me ASAP....thanks!
  85.  
  86. Sorry, I don't think I have a large enough envelope and I certainly can't 
  87. afford the postage.  However, I will email a copy of my reply to you :-)
  88.  
  89. -- 
  90. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  91. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  92.  
  93.